import 'package:shared_preferences/shared_preferences.dart';
import 'package:http/http.dart' as http;
import 'dart:async';
import 'dart:convert';
import '../model/UserInfo.dart';

class DataUtils {
    static final String SP_AC_TOKEN = "accessToken";
    static final String SP_UID = "uid";
    static final String SP_IS_LOGIN = "isLogin";
    
    static final String SP_USER_INFO = "userInfo";
    
    // 保存用户登录信息，data中包含了token等信息
    static saveLoginInfo(var data) async {
        if (data != null) {
            SharedPreferences sp = await SharedPreferences.getInstance();
            
            String accessToken = data['access_token'];
            await sp.setString(SP_AC_TOKEN, accessToken);
            
            num uid = data['id'];
            await sp.setInt(SP_UID, uid);
            
            await sp.setBool(SP_IS_LOGIN, true);

            await sp.setString(SP_USER_INFO, json.encode(data));
        }
    }
    
    static clearLoginInfo() async {
        SharedPreferences sp = await SharedPreferences.getInstance();
        await sp.setString(SP_AC_TOKEN, "");
        await sp.setInt(SP_UID, 0);
        await sp.setBool(SP_IS_LOGIN, false);
        await sp.setString(SP_USER_INFO, "");
    }
    
    // 获取用户信息
    static Future<UserInfo> getUserInfo() async {
        SharedPreferences sp = await SharedPreferences.getInstance();
        bool isLogin = sp.getBool(SP_IS_LOGIN);
        if (isLogin == null || !isLogin) {
            return null;
        }

        String strUserInfo = sp.getString(SP_USER_INFO);
        var mapUserInfo = json.decode(strUserInfo);
        
        UserInfo userInfo = new UserInfo();
        
        userInfo.id = mapUserInfo['id'];
        userInfo.relname = mapUserInfo['relname'];
        userInfo.phone = mapUserInfo['phone'];
        userInfo.icon = mapUserInfo['icon'];
        userInfo.status = mapUserInfo['status'];
        userInfo.imei = mapUserInfo['imei'];
        userInfo.access_user_token = mapUserInfo['access_token'];
        userInfo.out_time = mapUserInfo['out_time'];
        userInfo.sex = mapUserInfo['sex'];
        userInfo.role_id = mapUserInfo['role_id'];
        userInfo.copyright_id = mapUserInfo['copyright_id'];
        
        return userInfo;
    }
    
    static Future<bool> isLogin() async {
        SharedPreferences sp = await SharedPreferences.getInstance();
        bool b = sp.getBool(SP_IS_LOGIN);
        return b != null && b;
    }
    
    static Future<String> getAccessToken() async {
        SharedPreferences sp = await SharedPreferences.getInstance();
        return sp.getString(SP_AC_TOKEN);
    }

    static Future<String> netPost(String url, {Map<String, String> params}) async {
        SharedPreferences sp = await SharedPreferences.getInstance();
        Map<String, String> _headers = new Map();
        _headers['Content-Type'] = 'application/x-www-form-urlencoded';
        _headers['access_token'] = sp.getString(SP_AC_TOKEN);
        http.Response res = await http.post(url, body: params, headers: _headers);
        print(res);
        return res.body;
    }

    
}